/*
 * WorxKeyManager.java
 * 
 * Created on Mar 13, 2006
 * David Connell
 * Copyright (c) 2006 Synergy Systems, Inc
 */
package org.puresilk.afx.util;

import java.util.*;

/**
 * Global repository for JVM meta data.
 * 
 * Pattern: Singleton 
 * 
 * @author David Connell
 */
public class SilkAfxKeyManager {
	private static SilkAfxKeyManager kMgr = null;
	private static HashMap<String,Object> keys = null;
	
	/**
	 * Private constructor initializes the
	 * repository.
	 */
	private SilkAfxKeyManager( ) {
		keys = new HashMap<String,Object>();
	}
	
	/**
	 * Access point for all needers.
	 * 
	 * @return
	 */
	public static SilkAfxKeyManager getInstance() {
		if (kMgr == null)
			kMgr = new SilkAfxKeyManager();
			
		return kMgr;	
	}
	
	/**
	 * Place an object into the repository.
	 * 
	 * @param key
	 * @param value
	 */
	public void setKey(String key, Object value) {
		keys.put(key,value);
	}

	/** 
	 * Fetch an object from the repository.
	 * 
	 * @param key
	 * @return
	 */
	public Object getKey(String key){
		Object rv = null;
		if (keys.containsKey(key))
			rv = keys.get(key);
			
		return rv;	
	}
		
	/**
	 * Remove an object from repository.
	 * 
	 * @param key
	 */	
	public void removeKey(String key) {
		if (keys.containsKey(key))
			keys.remove(key);		
	}

	/**
	 * Retrieve an app property.
	 * 
	 * @param pKey
	 * @return
	 */
	public String getProperty(String pKey) {
		String rv = null;
		String prefix = "";
		String key = pKey;
		
		// check for compound (application) property
		String [] ar = pKey.split(".");
		if ( ar.length > 1 ) {
			prefix = ar[0] + ".";
			key = ar[1];
		}
		
		// get internal properties collection
		Properties props = (Properties)keys.get(prefix + "AppProperties");
		
		// extract value from key
		try {
			rv = (String)props.get(key);
		}
		catch(Exception e){
			rv = null;
		}
		return rv;
	}
}
